<template>
  <div class="wrapper">
    <!-- 头部 -->
    <header>
      <i class="fa fa-arrow-left" @click="goBack"></i>
      <div class="title">登录</div>
      <div class="placeholder"></div>
    </header>

    <!-- 登录表单 -->
    <div class="login-form">
      <!-- Logo -->
      <div class="logo">
        <img src="../assets/img/logo.png" alt="logo">
      </div>

      <!-- 表单输入区 -->
      <div class="form-group">
        <div class="input-group">
          <i class="fa fa-user"></i>
          <input 
            type="text" 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            @keyup.enter="handleLogin"
          >
        </div>
        <div class="input-group">
          <i class="fa fa-lock"></i>
          <input 
            :type="showPassword ? 'text' : 'password'" 
            v-model="loginForm.password" 
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
          >
          <i 
            :class="['fa', showPassword ? 'fa-eye' : 'fa-eye-slash']" 
            @click="showPassword = !showPassword"
          ></i>
        </div>
      </div>

      <!-- 登录按钮 -->
      <div class="login-btn" @click="handleLogin">
        登录
      </div>

      <!-- 其他选项 -->
      <div class="options">
        <div class="register" @click="router.push('/register')">注册账号</div>
        <div class="forget-pwd" @click="router.push('/forgot-password')">忘记密码？</div>
      </div>

      <!-- 其他登录方式 -->
      <div class="other-login">
        <div class="divider">
          <span class="line"></span>
          <span class="text">其他登录方式</span>
          <span class="line"></span>
        </div>
        <div class="login-methods">
          <div class="method">
            <i class="fa fa-wechat"></i>
            <p>微信</p>
          </div>
          <div class="method">
            <i class="fa fa-qq"></i>
            <p>QQ</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '../api/user'
import { useTokenStore } from '@/stores/token.js';
import { useUserInfoStore } from '@/stores/user.js';
import { getUserInfoAfterLogin } from '../api/user'
const router = useRouter()
const showPassword = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const handleLogin = async () => {
  try {
    if (!loginForm.username || !loginForm.password) {
      ElMessage.warning('请输入用户名和密码')
      return
    }

    const res = await login(loginForm)
    if (res.code === 0) {
      ElMessage.success('登录成功')
      sessionStorage.setItem('user', JSON.stringify(res.data))
      useTokenStore().setToken(res.data)
      let userInfo = await getUserInfoAfterLogin()
      useUserInfoStore().setUserInfo(userInfo.data)
      router.push('/index')
    } else {
      ElMessage.error(res.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请稍后重试')
  }
}

// 返回到“我的”页面
const goBack = () => {
  router.push('/profile')
}
</script>

<style scoped>
.wrapper {
  width: 100%;
  min-height: 100vh;
  background-color: #fff;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3vw 4vw;
  background-color: #fff;
}

header .fa-arrow-left {
  font-size: 4vw;
  color: #ff7043;
  width: 8vw;
  cursor: pointer;
}

header .title {
  font-size: 4.5vw;
  font-weight: bold;
}

header .placeholder {
  width: 8vw;
}

.login-form {
  padding: 8vw 6vw;
}

.logo {
  text-align: center;
  margin-bottom: 8vw;
}

.logo img {
  width: 60vw;
  height: 40vw;
}

.form-group {
  margin-bottom: 6vw;
}

.input-group {
  display: flex;
  align-items: center;
  padding: 3vw 4vw;
  background-color: #f8f8f8;
  border-radius: 2vw;
  margin-bottom: 4vw;
}

.input-group i {
  font-size: 5vw;
  color: #ff7043;
  margin-right: 3vw;
}

.input-group input {
  flex: 1;
  border: none;
  background: none;
  font-size: 3.8vw;
  color: #333;
  outline: none;
}

.input-group input::placeholder {
  color: #999;
}

.input-group .fa-eye,
.input-group .fa-eye-slash {
  margin-right: 0;
  cursor: pointer;
}

.login-btn {
  background-color: #ff7043;
  color: #fff;
  text-align: center;
  padding: 3vw;
  border-radius: 2vw;
  font-size: 4vw;
  margin-bottom: 4vw;
  cursor: pointer;
}

.options {
  display: flex;
  justify-content: space-between;
  color: #666;
  font-size: 3.5vw;
  margin-bottom: 8vw;
}

.options > div {
  cursor: pointer;
}

.other-login {
  text-align: center;
}

.divider {
  display: flex;
  align-items: center;
  margin-bottom: 6vw;
}

.divider .line {
  flex: 1;
  height: 1px;
  background-color: #eee;
}

.divider .text {
  color: #999;
  font-size: 3.5vw;
  padding: 0 3vw;
}

.login-methods {
  display: flex;
  justify-content: center;
  gap: 8vw;
}

.method {
  text-align: center;
  cursor: pointer;
}

.method i {
  font-size: 8vw;
  margin-bottom: 1vw;
}

.method .fa-wechat {
  color: #07c160;
}

.method .fa-qq {
  color: #12b7f5;
}

.method p {
  font-size: 3.2vw;
  color: #666;
}
</style>